﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MessageBox : UIPanelBase
{
    [SerializeField]
    private TextMeshProUGUI m_TitleLabel, m_ContentLabel, m_ConfirmLabel, m_CancelLabel;
    [SerializeField]
    private Button m_ConfirmButton, m_CancelButton;

    private Action m_ConfirmCallback, m_CancelCallback;

    protected override void OnInit()
    {
        m_ConfirmButton.onClick.AddListener(OnConfirmButton);
        m_CancelButton.onClick.AddListener(OnCancelButton);
    }

    protected override void OnShow()
    {
        m_ConfirmCallback = null;
        m_CancelCallback = null;
    }

    public static MessageBox ShowOK(string title = null, string content = null, string okLabel = null, Action onOK = null)
    {
        var box = UIManager.Find<MessageBox>();
        box.Show();
        box.SetBoxType(BoxType.OK);
        box.SetTitle(title);
        box.SetContent(content);
        box.SetConfirmLabel(okLabel);
        box.SetConfirmCallback(onOK);
        return box;
    }

    public static MessageBox ShowYesNo(string title = null, string content = null, string confirmLabel = null, string cancelLabel = null,
        Action onConfirm = null, Action onCancel = null)
    {
        var box = UIManager.Find<MessageBox>();
        box.Show();
        box.SetBoxType(BoxType.YesNo);
        box.SetTitle(title);
        box.SetContent(content);
        box.SetConfirmLabel(confirmLabel);
        box.SetCancelLabel(cancelLabel);
        box.SetConfirmCallback(onConfirm);
        box.SetCancelCallback(onCancel);
        return box;
    }

    public MessageBox SetBoxType(BoxType type = BoxType.OK)
    {
        m_CancelButton.gameObject.SetActive(type == BoxType.YesNo);
        return this;
    }

    public MessageBox SetTitle(string title = null)
    {
        if (title.IsEmpty())
        {
            m_TitleLabel.gameObject.SetActive(false);
            return this;
        }
        m_TitleLabel.gameObject.SetActive(true);
        m_TitleLabel.text = title;
        return this;
    }

    public MessageBox SetContent(string content = null)
    {
        if (content.IsEmpty())
        {
            m_ContentLabel.gameObject.SetActive(false);
            return this;
        }
        m_ContentLabel.gameObject.SetActive(true);
        m_ContentLabel.text = content;
        return this;
    }

    public MessageBox SetConfirmLabel(string label = null)
    {
        m_ConfirmLabel.text = label;
        return this;
    }

    public MessageBox SetCancelLabel(string label = null)
    {
        m_CancelLabel.text = label;
        return this;
    }

    public MessageBox SetConfirmCallback(Action callback = null)
    {
        m_ConfirmCallback = callback;
        return this;
    }

    public MessageBox SetCancelCallback(Action callback = null)
    {
        m_CancelCallback = callback;
        return this;
    }

    private void OnConfirmButton()
    {
        m_ConfirmCallback?.Invoke();
        Hide();
    }

    private void OnCancelButton()
    {
        m_CancelCallback?.Invoke();
        Hide();
    }

    public enum BoxType
    {
        OK,
        YesNo
    }
}
